package com.abbas.smartCity.ui;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.abbas.smartCity.R;
import com.abbas.smartCity.TestFragment;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.ui.govservice.GovSerMainActivity;
import com.abbas.smartCity.ui.kt.KHomeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 工具人主页
 */
public class HomeActivity extends BaseActivity {

    private ViewPager vp;//页面
    public static BottomNavigationView bnv;//底部导航

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected boolean enableUp() {
        return false;
    }

    @Override
    protected String setCustomTitle() {
        return "首页";
    }

    @Override
    protected void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        bnv = (BottomNavigationView) findViewById(R.id.bnv);
    }

    @Override
    protected void initData() {
        //页面数据初始化
        final List<Fragment> pagers = Arrays.asList(new HomeFragment(), new ServerFragment(), new TestFragment(), new DaFragment(), new MineFragment());
        //设置页面缓存数量，全部缓存
        vp.setOffscreenPageLimit(4);
        //设置适配器
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return pagers.get(position);
            }

            @Override
            public int getCount() {
                return pagers.size();
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    protected void initEvent() {
        //底部导航栏点击
        bnv.setOnNavigationItemSelectedListener(item -> {
            Objects.requireNonNull(getSupportActionBar()).setTitle(item.getTitle());
            switch (item.getItemId()){
                case R.id.home:
                    vp.setCurrentItem(0);
                    return true;
                case R.id.all_server:
                    vp.setCurrentItem(1);
                    return true;
                case R.id.smart:
                    vp.setCurrentItem(2);
                    return true;
                case R.id.da:
                    vp.setCurrentItem(3);
                    return true;
                case R.id.mine:
                    vp.setCurrentItem(4);
                    return true;
            }
            return false;
        });


        //startActivity(GovSerMainActivity.class);
    }
}